package com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.auto.ArchivesConfigItemMapper;
import com.mapper.auto.StuArchivesRelMapper;
import com.model.auto.ArchivesConfigItem;
import com.model.auto.StuArchivesRel;
import com.model.auto.vo.StuArchivesRelSaveVo;
import com.service.IStuArchivesRelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案信息配置信息 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2022-09-14
 */
@Slf4j
@Service
public class StuArchivesRelServiceImpl extends ServiceImpl<StuArchivesRelMapper, StuArchivesRel> implements IStuArchivesRelService {
    @Autowired
    private ArchivesConfigItemMapper archivesConfigItemMapper;


    @Override
    public boolean saveOrUpdateRel(List<StuArchivesRelSaveVo> stuArchivesRelSaveVo) {
        //查出我的
        Integer stuId = stuArchivesRelSaveVo.get(0).getStuId();
        List<StuArchivesRel> myStuArchivesRels = this.list(new LambdaQueryWrapper<StuArchivesRel>()
                .eq(StuArchivesRel::getStuId, stuId)
        );

        ArrayList<Object> infoIds = new ArrayList<>();
        stuArchivesRelSaveVo.forEach(i -> infoIds.add(i.getInfoId()));
        //查出所有的item
        List<ArchivesConfigItem> items = archivesConfigItemMapper.selectList(new LambdaQueryWrapper<ArchivesConfigItem>()
                .in(ArchivesConfigItem::getInfoId, infoIds)
        );


        ArrayList<Object> removeList = new ArrayList<>();
        ArrayList<JSONObject> addList = new ArrayList<>();


        for (StuArchivesRel myStuArchivesRel : myStuArchivesRels) {
            boolean isDel = true;
            for (StuArchivesRelSaveVo archivesRelSaveVo : stuArchivesRelSaveVo) {
                if (myStuArchivesRel.getInfoId().equals(archivesRelSaveVo.getInfoId())) {
                    for (String integer : archivesRelSaveVo.getCodeList()) {
                        boolean isAdd = true;
                        for (ArchivesConfigItem item : items) {
                            if (item.getCode().equals(integer.toString())
                                    && item.getInfoId().equals(archivesRelSaveVo.getInfoId())) {
                                if (myStuArchivesRel.getItemId().equals(item.getId())) {
                                    isDel = false;
                                    break;
                                }
                            }
                        }
                        if (!isDel) break;
                    }
                }
                if (!isDel) break;
            }
            if (isDel) {
                removeList.add(myStuArchivesRel.getId());
            }
        }

        for (StuArchivesRelSaveVo archivesRelSaveVo : stuArchivesRelSaveVo) {
            for (String integer : archivesRelSaveVo.getCodeList()) {
                boolean isAdd = true;
                for (StuArchivesRel myStuArchivesRel : myStuArchivesRels) {
                    if (archivesRelSaveVo.getInfoId().equals(myStuArchivesRel.getInfoId())) {
                        for (ArchivesConfigItem item : items) {
                            if (item.getCode().equals(integer.toString())
                                    && item.getInfoId().equals(archivesRelSaveVo.getInfoId())) {
                                if (myStuArchivesRel.getItemId().equals(item.getId())) {
                                    isAdd = false;
                                }
                            }
                            if (!isAdd) break;
                        }
                    }
                    if (!isAdd) break;
                }
                if (isAdd) {
                    JSONObject object = new JSONObject();
                    object.put("code", integer);
                    object.put("infoId", archivesRelSaveVo.getInfoId());
                    addList.add(object);
                }
            }
        }

        //删除ok
        System.out.println("删除" + removeList);
        System.out.println("新增" + addList);


        if (removeList.size() > 0) {
            boolean remove = this.update(new LambdaUpdateWrapper<StuArchivesRel>()
                    .in(StuArchivesRel::getId, removeList)
                    .eq(StuArchivesRel::getStuId, stuId)
                    .set(StuArchivesRel::getEnabled, 1)
            );
            log.info("删除:{}", remove ? "成功" : "失败");
        }
        if (addList.size() > 0) {
            ArrayList<StuArchivesRel> saveList = new ArrayList<>();
            for (ArchivesConfigItem item : items) {
                for (JSONObject object : addList) {
                    String code = object.getString("code");
                    Integer infoId = object.getInteger("infoId");
                    if (item.getInfoId().equals(infoId)) {
                        if (item.getCode().equals(code)) {
                            StuArchivesRel archivesRel = new StuArchivesRel();
                            archivesRel.setItemId(item.getId());
                            archivesRel.setInfoId(infoId);
                            archivesRel.setStuId(stuId);
                            saveList.add(archivesRel);
                        }
                    }
                }
            }
            boolean b = this.saveBatch(saveList);
            log.info("新增:{}", b ? "成功" : "失败");
            return b;
        }

        return false;
    }

    @Override
    public Map<String, Object> getNotAdmitted(Integer stuid) {
        HashMap<String, Object> map = new HashMap<>();
        List<StuArchivesRel> list = this.list(new LambdaQueryWrapper<StuArchivesRel>()
                .eq(stuid != null, StuArchivesRel::getStuId, stuid)
                .eq(StuArchivesRel::getEnabled, 0)
                .or((i) -> {
                    i.isNull(StuArchivesRel::getEnabled);
                })
        );
        map.put("stuList", list);
        ArrayList itemIds = new ArrayList<>();
        list.forEach(i -> itemIds.add(i.getItemId()));

        List<ArchivesConfigItem> items = itemIds.size() == 0 ? new ArrayList<>() :
                archivesConfigItemMapper.selectList(new LambdaQueryWrapper<ArchivesConfigItem>()
                        .select(ArchivesConfigItem::getCode, ArchivesConfigItem::getInfoId,
                                ArchivesConfigItem::getId)
                        .in(ArchivesConfigItem::getId, itemIds)
                );
        map.put("xz", items);
        return map;
    }
}
